package com.zkzx.driver.service;

import com.zkzx.common.driver.command.DriverCommand;
import com.zkzx.common.driver.model.DriverQualificationVO;
import com.zkzx.driver.mapper.DriverQualificationMapper;
import com.zkzx.driver.mapper.entity.DriverQualification;
import com.zkzx.driver.mapper.entity.DriverQualificationExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 *
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/08/10:24
 * @Description:
 */
@Service
public class DriverQualificationService {

    @Resource
    private DriverQualificationMapper driverQualificationMapper;

    /**
     * 根据司机id查询银行卡信息
     * @param driverId 企业id
     * @return 银行卡信息
     */
    public DriverQualificationVO findVOByDriverId(Integer driverId){
        DriverQualification driverQualification = findByDriverId(driverId);
        if(Objects.isNull(driverQualification)){
            return null;
        }
        return DriverQualificationVO.builder()
                .code(driverQualification.getCode())
                .startTime(driverQualification.getStarttime())
                .endTime(driverQualification.getEndtime())
                .pic(driverQualification.getPic())
                .contractNum(driverQualification.getContractnum())
                .contractStatus(driverQualification.getContractstatus())
                .build();
    }

    /**
     * 根据司机id查询银行卡信息
     * @param driverId 企业id
     * @return 银行卡信息
     */
    public DriverQualification findByDriverId(Integer driverId){
        DriverQualificationExample driverQualificationExample = new DriverQualificationExample();
        driverQualificationExample.createCriteria().andDriveridEqualTo(driverId);
        Optional<DriverQualification> driverQualificationOne = driverQualificationMapper.selectByExample(driverQualificationExample).stream().findFirst();

        DriverQualification driverQualification  = null;
        if(driverQualificationOne.isPresent()){
            driverQualification = driverQualificationOne.get();
        }
        return driverQualification;
    }

    /**
     * 更新资格证信息
     * @param command 入参
     * @param id 司机id
     */
    public void update(DriverCommand command, Integer id){
        if(Objects.isNull(command.getDriverQualification())){
            return;
        }
        DriverQualification driverQualification = new DriverQualification();
        driverQualification.setId(command.getDriverQualification().getId());
        driverQualification.setCode(command.getDriverQualification().getCode());
        driverQualification.setStarttime(command.getDriverQualification().getStartTime());
        driverQualification.setEndtime(command.getDriverQualification().getEndTime());
        driverQualification.setPic(command.getDriverQualification().getPic());
        driverQualification.setContractnum(command.getDriverQualification().getContractNum());
        driverQualification.setContractstatus(command.getDriverQualification().getContractStatus());
        DriverQualificationExample driverQualificationExample = new DriverQualificationExample();
        DriverQualificationExample.Criteria criteriaQualification = driverQualificationExample.createCriteria();
        criteriaQualification.andDriveridEqualTo(id);
        driverQualificationMapper.updateByExampleSelective(driverQualification,driverQualificationExample);
    }

    /**
     * 插入资格证信息
     * @param command 入参
     * @param id 司机id
     */
    public void insert(DriverCommand command, Integer id){
        if(Objects.isNull(command.getDriverQualification())){
            return;
        }
        DriverQualification driverQualification = new DriverQualification();
        driverQualification.setDriverid(id);
        Optional.ofNullable(command.getDriverQualification()).ifPresent(dq->{
            driverQualification.setCode(command.getDriverQualification().getCode());
            driverQualification.setStarttime(command.getDriverQualification().getStartTime());
            driverQualification.setEndtime(command.getDriverQualification().getEndTime());
            driverQualification.setPic(command.getDriverQualification().getPic());
            driverQualification.setContractnum(command.getDriverQualification().getContractNum());
            driverQualification.setContractstatus(command.getDriverQualification().getContractStatus());

        });
        driverQualificationMapper.insert(driverQualification);
    }


    public void insertOrUpdateQualification(DriverCommand command, Integer id){
        if (Objects.nonNull(command.getDriverQualification())){
            DriverQualification driverQualification = new DriverQualification();
            driverQualification.setDriverid(id);
            driverQualification.setCode(command.getDriverQualification().getCode());
            driverQualification.setStarttime(command.getDriverQualification().getStartTime());
            driverQualification.setEndtime(command.getDriverQualification().getEndTime());
            driverQualification.setPic(command.getDriverQualification().getPic());
            driverQualification.setContractnum(command.getDriverQualification().getContractNum());
            driverQualification.setContractstatus(command.getDriverQualification().getContractStatus());

            DriverQualification byDriverId = this.findByDriverId(id);

            if(Objects.isNull(byDriverId)){
                driverQualificationMapper.insert(driverQualification);
                return;
            }

            DriverQualificationExample driverQualificationExample = new DriverQualificationExample();
            driverQualificationExample.createCriteria().andIdEqualTo(byDriverId.getId());
            driverQualificationMapper.updateByExampleSelective(driverQualification,driverQualificationExample);

        }

    }
}
